package com.leoandroid.tool.toolsbox.new

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import com.leoandroid.tool.toolsbox.adapter.CurrencyExchangeAdapter
import com.leoandroid.tool.toolsbox.bean.CurrExchangeBean
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityCurrencyexchangeBinding
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.Exception
import java.lang.StringBuilder

class CurrencyExchangeActivity: AppCompatActivity() {

    var bindF:ActivityCurrencyexchangeBinding?=null
    val binding get() = bindF!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindF = ActivityCurrencyexchangeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.includeTitle.titleTv.text = "货币汇率"
        binding.includeTitle.backIv.setOnClickListener {
            finish()
        }
        val str = getConfigJson(this)
        setConfigJson(str)
        Log.d("pppppp","str>>"+str)
        val fromJson = Gson().fromJson(str.toString(), CurrExchangeBean::class.java)
        val adapter = CurrencyExchangeAdapter(this@CurrencyExchangeActivity,fromJson.exchange)
                    binding.recycleview.layoutManager = LinearLayoutManager(this@CurrencyExchangeActivity)
                    binding.recycleview.adapter = adapter
//        val url = "http://web.juhe.cn/finance/exchange/rmbquot?key=95329e9f169c8d90a33e193151a80217"
//        NetRequest.asyncGet(url,object:NetCallBack{
//            override fun onFailure(code: Int, e: Throwable?) {
//
//            }
//
//            override fun onSuccess(json: JSONObject?) {
//                val dataList = ArrayList<CurrExchangeBean.ExchangeBean>()
//                val string = json?.getString("result")
//                val jsonResult = JSONArray(string)
//                val jsonObject = jsonResult[0]
//                //val data = jsonResult.getString("data")
//                Log.d("pppppp","jsonObject>>"+jsonObject.toString());
//                //val fromJson = Gson().fromJson(jsonObject.toString(), CurrExchangeBean::Class.java)
//                val data = JSONArray(jsonObject)
//                for (index in 0 until data.length()){
//                    val jsonArr = data[index]
//                    val d = JSONObject(jsonArr.toString())
//                    val name = d.getString("name")
//                    val mSellPri = d.getString("mSellPri")
//                    val mBuyPri = d.getString("mBuyPri")
//                    val fSellPri = d.getString("fSellPri")
//                    val fBuyPri = d.getString("fBuyPri")
//                    val date = d.getString("date")
//                    val time = d.getString("time")
//                    val bankConversionPri = d.getString("bankConversionPri")
//                    val bean = CurrExchangeBean.ExchangeBean(name,fBuyPri,mBuyPri,fSellPri,mSellPri,bankConversionPri,date,time)
//                    dataList.add(bean)
//                }
//                runOnUiThread {
//                    val adapter = CurrencyExchangeAdapter(this@CurrencyExchangeActivity,dataList)
//                    binding.recycleview.layoutManager = LinearLayoutManager(this@CurrencyExchangeActivity)
//                    binding.recycleview.adapter = adapter
//                }
//            }
//
//        })

    }


    fun setConfigJson(content: String?) {
        MMKV.defaultMMKV().encode("hui:json", content)
    }

    fun getConfigJson(context: Context): String? {
        val fileName = "hui.json"
        var json: String? = MMKV.defaultMMKV().decodeString("hui:json")
        if (TextUtils.isEmpty(json)) {
            json = readAssetsJson(context, fileName)
        }
        return json
    }

    private fun readAssetsJson(context: Context, fileName: String): String? {
        var json = ""
        try {
            val inputStreamReader = InputStreamReader(context.resources.assets.open(fileName))
            val bufferedReader = BufferedReader(inputStreamReader)
            var line: String?
            val stringBuilder = StringBuilder()
            while (bufferedReader.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }
            inputStreamReader.close()
            bufferedReader.close()
            json = stringBuilder.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return json
    }

}